package com.tevintao.marketingsystem.task;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.provider.ChatMessageBean;
import com.tevintao.marketingsystem.provider.ChatNewChatDB;
import com.tevintao.marketingsystem.provider.ChatNewMessageProvider;
import com.tevintao.marketingsystem.provider.LastUpdateDB;
import com.tevintao.marketingsystem.services.TaskLogicService;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;


/**
 * 自己人聊天
 */
public class ReadNewMessageProcess extends SetTaskExtra {
    private static ReadNewMessageProcess ourInstance = new ReadNewMessageProcess();

    public static ReadNewMessageProcess getInstance() {
        return ourInstance;
    }

    private ReadNewMessageProcess() {
    }

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {

            View chatViewGroupView = v.findViewById(WxUiViewConfig.CHAT_VIEW_GROUP_VIEW);
            if (null != chatViewGroupView && chatViewGroupView.getVisibility() == View.VISIBLE) {
                //聊天界面已经打开
                LogManager.getInstance().logInfo("聊天界面已经打开");
                TaskLogicService.sendKeyEvent(v.getContext(), TaskLogicService.INPUT_TYPE_BACK);
                return true;
            }

            WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_WEIXIN);

            View tagView = v.findViewWithTag(WxUiViewConfig.TAB_WEIXIN);
            if (null != tagView) {
                View newMessageNumView = tagView.findViewById(WxUiViewConfig.TAB_ICO_NEW_MESSAGE_NUM);
                if (null != newMessageNumView && newMessageNumView.getVisibility() == View.VISIBLE) { //有新消息 在微信TAB页面

                    boolean findSuccess = false;
                    View chatListView = v.findViewById(WxUiViewConfig.CHAT_LIST_VIEW);
                    if (null != chatListView && chatListView instanceof ListView) {
                        ListView chatLV = (ListView) chatListView;

                        for (int i = 0, len = chatLV.getChildCount(); i < len; i++) {
//                            LogManager.getInstance().logInfo("---------------------------------------------------------------");
                            View childView = chatLV.getChildAt(i);
                            View chatNewNum = childView.findViewById(WxUiViewConfig.LIST_ITEM_NEW_MESSAGE_NUM);
                            if (null != chatNewNum && chatNewNum.getVisibility() == View.VISIBLE && chatNewNum instanceof TextView) {
                                TextView newTextNumView = (TextView) chatNewNum;
                                if (!TextUtils.isEmpty(newTextNumView.getText().toString())) {
                                    ViewOperation.clickView(childView);
                                    findSuccess = true;
                                    return true;
                                }
                            }
//                            LogManager.getInstance().logInfo("---------------------------------------------------------------");
                        }
                    }
                    if (!findSuccess && null != newMessageNumView && newMessageNumView.getVisibility() == View.VISIBLE) {
                        ViewOperation.clickView(newMessageNumView);
                    }
                } else { //没有消息，结束任务
                    taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "消息阅读完毕");
                    return true;
                }
            }
        }else if (WxUiViewConfig.VIEW_TENCENT_NEWS.equals(contextName)) {
            TaskLogicService.sendKeyEvent(v.getContext(), TaskLogicService.INPUT_TYPE_BACK);
            return true;
        }
        return false;
    }

}
